/**
 * Created by machan on 2017/4/28.
 */

import UmfLog from './UmfLog';


/**
 * 页面跳转路由
 */
class PageRouter {
    /**
     * 跳转传值 新页面加startPageForData（data, from）方法，或者会setData（data）
     * @param obj
     * ex.
     * {
     *    url:
     *    data:
     * }
     */
    navigateTo(obj) {
        wx.navigateTo({
            url: obj.url,
            success: function (res) {
                obj.success && obj.success(res);
                if (obj.data) {
                    setTimeout(() => {
                        let pages = getCurrentPages();
                        let curPage = pages[pages.length - 1];
                        if(curPage.startPageForData) {
                            curPage.startPageForData(obj.data, obj.from);
                        } else {
                            curPage.setData(obj.data);
                        }
                    }, 1000);
                }
            },
            fail: function (err) {
                obj.fail && obj.fail(err);
            },
            complete: function () {
                obj.complete && obj.complete();
            }
        });
    }

    /**
     * 返回并回传值 原页面加pageForResult（data, from）方法，或者会setData（data）
     * @param obj
     * ex.
     * {
     *    delta:
     *    data:
     * }
     */
    navigateBack(obj) {
        let delta = obj.delta ? obj.delta : 1;
        if (obj.data) {
            let pages = getCurrentPages();
            let curPage = pages[pages.length - (delta + 1)];
            if(curPage.pageForResult) {
                curPage.pageForResult(obj.data, obj.from);
            } else {
                curPage.setData(obj.data);
            }
        }
        wx.navigateBack({
            delta: delta,
            success: function (res) {
                // success
                obj.success && obj.success(res);
            },
            fail: function (err) {
                // fail
                obj.fail && obj.fail(err);
            },
            complete: function () {
                // complete
                obj.complete && obj.complete();
            }
        });
    }

    /**
     * 跳转回某个页面
     * @param name
     */
    navigateBackTo(name) {
        let pages = getCurrentPages();
        let delta;
        for (let index in pages) {
            let page = pages[index];
            if (page.route === name) {
                delta = index;
            }
        }
        wx.navigateBack({
            delta: delta,
        });
    }

    /**
     * 在任意页面跳转到指定页面
     * @param pageName
     * @param params
     * @param onPageSameCB
     */
    anyNavigateTo(pageName, params, onPageSameCB) {
        let pages = getCurrentPages();
        let curPage = pages[pages.length -1];
        let route = curPage.route;
        if (route === pageName) {
            UmfLog.log("相同页面，取消跳转");
            onPageSameCB(curPage);
            return;
        }
        let routes = route.split("/");
        let preUrl = '';
        for (let index = 1; index<routes.length; index++) {
            preUrl += "../";
        }
        let url = preUrl + pageName + (params?params:'');
        wx.navigateTo({
            url: url
        });
    }

    /**
     * 封装wx.navigateTo()
     * @param page
     * @param url
     * @param isRedirect
     * @param params
     */
    wxNavigateTo(page, url, isRedirect = false, params) {
        let pageName = '';
        if (typeof page === 'string') {
            pageName = page;
        } else {
            let pIndexs = page.route.split('\/');
            pageName = pIndexs[pIndexs.length - 1];
        }

        params = Object.assign({
            fromPage: pageName
        }, params);
        //组装url
        let mUrl = '';
        if (url.indexOf('?') >= 0) {
            mUrl = url + "&params=" + JSON.stringify(params)
        } else {
            mUrl = url + "?params=" + JSON.stringify(params)
        }
        //跳转
        if (isRedirect) {
            wx.redirectTo({
                url: mUrl
            });
        } else {
            wx.navigateTo({
                url: mUrl
            });
        }
    }

}
PageRouter = new PageRouter();
module.exports = PageRouter;